﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public class Test1
    {
        public delegate void MethodDelegate();
        public delegate void MethodDelegate1(String name);

        //1.无参无返回值
        public void SayHello()
        {
            Console.WriteLine("Hello");
        }

        //2.无参有返回值
        public void SayHello(String name)
        {
            Console.WriteLine($"Hello {name}");
        }

        public void RunTest()
        {
            //MethodDelegate md1 = new MethodDelegate(SayHello);
            //md1.Invoke();

            //MethodDelegate md1 = SayHello;//md1作为SayHello的经纪人。
            //md1.Invoke();

            MethodDelegate1 md2 = SayHello;
            //md2.Invoke("John");
            md2("Zhangsan");
        }

        //3.有参数有返回值(计算两个数的加法运算)
        public void Add(int a, int b)
        {
            Console.WriteLine(a + b);
        }

        public void Sub(int a, int b)
        {
            Console.WriteLine(a - b);
        }
        public void Mul(int a, int b)
        {
            Console.WriteLine(a * b);
        }
        public void Div(int a, int b)
        {
            Console.WriteLine(a / b);
        }

        public delegate void MethodDelegate2(int a, int b);

        public void Run2()
        {
            MethodDelegate2 md2 = Add;
            md2 += Sub;
            md2 += Mul;
            md2 += Div;

            md2 -= Mul;

            md2.Invoke(6, 3);
        }
    }
}
